package com.fsq.controller;

import com.fsq.beans.vo.request.EmployeePost;
import com.fsq.beans.vo.request.EmployeePut;
import com.fsq.beans.vo.response.EmployeeRes;
import com.fsq.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author fsq
 * @date 2020/07/27
 */
@RestController
@Validated
@RequestMapping("/employees")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping
    public List<EmployeeRes> listEmployee() {
        return employeeService.listEmployees();
    }

    @GetMapping("/{id:\\d+}")
    public EmployeeRes get(@PathVariable(value = "id") Integer id) {
        return employeeService.getById(id);
    }

    @PutMapping
    public void update(@Valid @RequestBody EmployeePut employee) {
        employeeService.update(employee);
    }

    @PostMapping
    public void insert(@Valid @RequestBody EmployeePost employee) {
        employeeService.insert(employee);
    }

    @DeleteMapping("/{id:\\d+}")
    public void delete(@PathVariable(value = "id") Integer id) {
        employeeService.delete(id);
    }


}
